module ApplicationHelper
=begin
  def render_page_title
    site_name = Setting.app_name
    title = @page_title ? "#{site_name} | #{@page_title}" : site_name rescue "SITE_NAME"
    content_tag("title", title, nil, false)
  end
=end

  def quality_count
    count = Movie.verified.count
  end

  def current_user
    if session[:user_id]
      @current_user ||= User.find(session[:user_id])
    else
      nil
    end
  end

  def admin
    @user = current_user
    return @user.admin
  end

  def movie_tags_tag(movie, field_name, opts = {})

    base_movies_path_url = action_name == "series" ? series_movies_path : movies_path
    base_movies_path_url = search_movies_path if action_name == 'search'
    base_movies_path_url = top_movies_path if action_name == 'top'

    return "" if movie.blank? or field_name.blank?
    spliter = opts[:spliter] || " / "
    limit = opts[:limit] || 0
    field_name = field_name.to_s.downcase.tableize
    items = movie.send("#{field_name}")
    items = [] if items.blank?
    items = items[0,limit] if limit > 0

    raw items.collect { |tag|
      #link_to(tag, "#{base_movies_path_url}?#{field_name.singularize}=#{tag}", :class => field_name.singularize,:title => tag)

      link_to(tag, movie_list_filter_url(field_name.singularize,tag), :class => field_name.singularize,:title => tag)
    }.join(spliter)
  end

  def movie_title_tag(movie, opts = {})
    allow_year = opts[:allow_year] || false
    return "" if movie.blank?
    title = movie.title
    class_name = ""
    link_title = movie.title
    if allow_year and not movie.year.blank?
      if movie.year > 0
        title = %{#{movie.title} <span class="year">(#{movie.year})</span>}
      else
        title = %{#{movie.title}}
      end
    end
    return link_to raw(title), movie_path(movie), :title => link_title, :class => class_name
  end


  def movie_title_tag_no_link(movie, opts = {})
    allow_year = opts[:allow_year] || false
    return "" if movie.blank?
    title = movie.title
    class_name = ""
    link_title = movie.title
    if allow_year and not movie.year.blank?
      if movie.year > 0
        title = %{<span class="title">#{movie.title}</span> <span class="year">(#{movie.year})</span>}
      else
        title = %{<span class="title">#{movie.title}</span>}
      end
    end
     return raw(title)
  end

  def render_page_title
    title = @page_title ? "#{@page_title} | #{Setting.app_name}" : Setting.app_name
    content_tag("title", title, nil, false)
  end

  def format_time(time)
    time.strftime("%Y-%m-%d %H:%M:%S")
  end

  def format_date(time)
    time.strftime("%Y.%m.%d")
  end

  def human_size_to_number(number)
    new_number=number*1024*1024*1024
    new_number.to_i
  end


  def filter_tag()

    t ||= Time.new
    num ||= t.strftime("%Y").to_i
    year = []
    end_num = num-15
    num.downto(end_num){|i| year << [i.to_s,i.to_s]}
    year.insert 0,[t('filter.all'),""]

    @category ||= Category.desc(:number).limit(15)
    @country ||= Country.desc(:number).limit(15)
    category = @category.map{|a| [a.name,a.name]}
    country = @country.map{|a| [a.name,a.name]}
    category.insert 0,[t('filter.all'),""]
    country.insert 0,[t('filter.all'),""]

    filter_data = {
      :country => [t('filter.country'),country],
      :category => [t('filter.category'),category],
      :year => [t('filter.year'),year]
    }
    return filter_data
  end


  def filter_sort()
    sort = [[t('filter.release'),:recent],[t('filter.upload'),:upload],[t('filter.score'),:rank]]
    return sort
  end



  def movie_list_filter_url(key = nil,value = nil)
    base_movies_path_url = action_name == "series" ? series_movies_path : movies_path
    base_movies_path_url = search_movies_path if action_name == 'search'
    base_movies_path_url = top_movies_path if action_name == 'top'
    return base_movies_path_url if key.blank? && value.blank?
    filters = params.clone
    filters.delete("action")
    filters.delete("controller")
    filters.delete(key)
    filters.delete("page")
    filters.delete("id")
    filter_strs = filters.collect { |param| "#{param[0]}=#{param[1]}" }
    if not value.blank?
      filter_strs << "#{key}=#{value}"
    end
    param_str = filter_strs.join("&")
    if !param_str.blank?
      "#{base_movies_path_url}?#{param_str}"
    else
      base_movies_path_url
    end
  end
end
